package springbootjedis.wqlbootjedis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import springbootjedis.wqlbootjedis.dao.DeptDao;
import springbootjedis.wqlbootjedis.entity.Dept;
import springbootjedis.wqlbootjedis.service.DeptService;

import java.util.concurrent.TimeUnit;

/**
 * @author wql
 * @version 1.0
 * @date 2022/12/7 14:05
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptDao deptDao;

    @Autowired
    private RedisTemplate<String ,Object> redisTemplate;
    @Override
    //查询
    @Cacheable(cacheNames="redis::dept",key="#id")
    public Dept SelectById(Integer id) {
        Dept dept = deptDao.selectById(id);
        return dept;
    }

    @Override
    @CacheEvict(cacheNames = "redis::dept",key = "#id")
    public Integer delete(Integer id) {
        redisTemplate.delete("dept::"+id);
        int i = deptDao.deleteById(id);
        return i;
    }

    @Override
    public Dept save(Dept dept) {
        int insert = deptDao.insert(dept);
        return dept;
    }

    @Override
    @CachePut(cacheNames = "redis::dept",key = "#dept.id")
    public Dept update(Dept dept) {
        deptDao.updateById(dept);
        ValueOperations<String, Object> value = redisTemplate.opsForValue();
        value.set("dept::"+dept.getId(),dept,3600,TimeUnit.SECONDS);
        return dept;
    }
}
